package cn.ycc1.boot3tx.service.impl;

import cn.ycc1.boot3tx.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ycc1.boot3tx.domain.Account;
import cn.ycc1.boot3tx.mapper.AccountMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
* @author mi
* @description 针对表【account】的数据库操作Service实现
* @createDate 2024-12-17 20:55:58
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
    implements AccountService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void decreaseBalance(Integer userId, BigDecimal amount) {
        Account account = this.getById(userId);
        if(account.getBalance().compareTo(amount) < 0){
            throw new RuntimeException("余额不足");
        }
        account.setBalance(account.getBalance().subtract(amount));
        this.updateById(account);
    }
}




